<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">


<link rel="stylesheet" href="/myblog/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/myblog/lib/pace/pace-theme-minimal.min.css">
  <script src="/myblog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gao-qianwei.gitee.io","root":"/myblog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Shell概述&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;Shell是一个命令行解释器，它接受应用程序&#x2F;用户命令，然后调用操作系统内核。">
<meta property="og:type" content="article">
<meta property="og:title" content="shell入门">
<meta property="og:url" content="https://gao-qianwei.gitee.io/myblog/2021/05/06/Shell%EF%BC%9AShell%E5%85%A5%E9%97%A8%E7%9F%A5%E8%AF%86/index.html">
<meta property="og:site_name" content="学无止境">
<meta property="og:description" content="Shell概述&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;Shell是一个命令行解释器，它接受应用程序&#x2F;用户命令，然后调用操作系统内核。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gao-qianwei.gitee.io/myblog/2021/05/06/Shell%EF%BC%9AShell%E5%85%A5%E9%97%A8%E7%9F%A5%E8%AF%86/shell.png">
<meta property="article:published_time" content="2021-05-06T13:03:50.000Z">
<meta property="article:modified_time" content="2021-05-07T11:14:05.565Z">
<meta property="article:author" content="高乾威">
<meta property="article:tag" content="shell">
<meta property="article:tag" content="linux">
<meta property="article:tag" content="leetcode">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gao-qianwei.gitee.io/myblog/2021/05/06/Shell%EF%BC%9AShell%E5%85%A5%E9%97%A8%E7%9F%A5%E8%AF%86/shell.png">

<link rel="canonical" href="https://gao-qianwei.gitee.io/myblog/2021/05/06/Shell%EF%BC%9AShell%E5%85%A5%E9%97%A8%E7%9F%A5%E8%AF%86/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>shell入门 | 学无止境</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">学无止境</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/myblog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/myblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-links">

    <a href="/myblog/links/" rel="section"><i class="fa fa-link fa-fw"></i>友链</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gao-qianwei.gitee.io/myblog/2021/05/06/Shell%EF%BC%9AShell%E5%85%A5%E9%97%A8%E7%9F%A5%E8%AF%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/myblog/images/avatar.png">
      <meta itemprop="name" content="高乾威">
      <meta itemprop="description" content="报我楼成秋望月，把君诗读夜回灯">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          shell入门
        </h1>

        <div class="post-meta">

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-05-06 21:03:50" itemprop="dateCreated datePublished" datetime="2021-05-06T21:03:50+08:00">2021-05-06</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-05-07 19:14:05" itemprop="dateModified" datetime="2021-05-07T19:14:05+08:00">2021-05-07</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/shell/" itemprop="url" rel="index"><span itemprop="name">shell</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <span class="post-meta-divider">|</span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="Shell概述"><a href="#Shell概述" class="headerlink" title="Shell概述"></a>Shell概述</h3><p>&ensp;&ensp;&ensp;&ensp;Shell是一个命令行解释器，它接受应用程序/用户命令，然后调用操作系统内核。</p>
<span id="more"></span>

<p><img src="/myblog/2021/05/06/Shell%EF%BC%9AShell%E5%85%A5%E9%97%A8%E7%9F%A5%E8%AF%86/shell.png"></p>
<p>&ensp;&ensp;&ensp;&ensp;Shell还是一个功能相当强大的编程语言，易编写、易调试、灵活性强。</p>
<p><a target="_blank" rel="noopener" href="https://www.runoob.com/linux/linux-shell.html">Shell 教程</a></p>
<h3 id="Shell解析器"><a href="#Shell解析器" class="headerlink" title="Shell解析器"></a>Shell解析器</h3><ul>
<li><p>Linux提供的shell解析器</p>
  <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ sudo cat /etc/shells</span><br><span class="line">[sudo] james 的密码： </span><br><span class="line"><span class="meta">#</span><span class="bash"> /etc/shells: valid login shells</span></span><br><span class="line">/bin/sh</span><br><span class="line">/bin/bash</span><br><span class="line">/bin/rbash</span><br><span class="line">/bin/dash</span><br></pre></td></tr></table></figure>
</li>
<li><p>bash和sh的关系（sh调用bash）</p>
  <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">root@james-Vostro-5471:/bin# ll | grep bash</span><br><span class="line">-rwxr-xr-x  1 root root 1113504 6月   7  2019 bash*</span><br><span class="line">lrwxrwxrwx  1 root root       4 3月  11  2020 rbash -&gt; bash*</span><br></pre></td></tr></table></figure></li>
<li><p>SUSE默认的shell解析器是bash</p>
  <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ echo $SHELL</span><br><span class="line">/bin/bash</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="Shell脚本入门"><a href="#Shell脚本入门" class="headerlink" title="Shell脚本入门"></a>Shell脚本入门</h3><h4 id="脚本格式"><a href="#脚本格式" class="headerlink" title="脚本格式"></a>脚本格式</h4><p>&ensp;&ensp;&ensp;&ensp;脚本以#！/bin/bash开头（指定解析器）</p>
<p>&ensp;&ensp;&ensp;&ensp;范例：创建一个shell脚本，输出helloworld</p>
<p>&ensp;&ensp;&ensp;&ensp;创建文件</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ touch helloworld.sh</span><br><span class="line">james@james-Vostro-5471:~/datas$ vim helloworld.sh</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;文件内容:</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">!/bin/bash</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;hello world&quot;</span></span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ sh helloworld.sh</span><br><span class="line">hello world</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash helloworld.sh</span><br><span class="line">hello world</span><br></pre></td></tr></table></figure>

<h4 id="脚本的常用执行方式"><a href="#脚本的常用执行方式" class="headerlink" title="脚本的常用执行方式"></a>脚本的常用执行方式</h4><p>&ensp;&ensp;&ensp;&ensp;第一种：采用bash或sh+脚本的相对路径或绝对路径（不用赋予脚本+x权限）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">root@james-Vostro-5471:/home/james/datas# sh helloworld.sh</span><br><span class="line">hello world</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# sh /home/james/datas/helloworld.sh</span><br><span class="line">hello world</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;第二种：采用输入脚本的绝对路径或相对路径执行脚本（必须具有可执行权限+x）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">root@james-Vostro-5471:/home/james/datas# ./helloworld.sh</span><br><span class="line">bash: ./helloworld.sh: 权限不够</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# ll | grep helloworld.sh</span><br><span class="line">-rw-r--r--  1 james james   32 5月   6 13:06 helloworld.sh</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# chmod 777 helloworld.sh</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# ll |grep helloworld.sh</span><br><span class="line">-rwxrwxrwx  1 james james   32 5月   6 13:06 helloworld.sh*</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# ./helloworld.sh</span><br><span class="line">hello world</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# /home/james/datas/helloworld.sh</span><br><span class="line">hello world</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;第一种执行方法，本质是bash解析器帮你执行脚本，所以脚本本身不需要执行权限。第二种方法，本质是脚本需要自己执行，所以需要执行权限。</p>
<h4 id="多命令处理"><a href="#多命令处理" class="headerlink" title="多命令处理"></a>多命令处理</h4><p>&ensp;&ensp;&ensp;&ensp;在/home/james/datas目录下创建一个demo.txt 在demo.txt文件中增加“this is a demo”：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">root@james-Vostro-5471:/home/james/datas# touch batch.sh</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# vim batch.sh</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# ll</span><br><span class="line">总用量 16</span><br><span class="line">drwxr-xr-x  2 james james 4096 5月   6 13:27 ./</span><br><span class="line">drwxr-xr-x 22 james james 4096 5月   6 13:06 ../</span><br><span class="line">-rw-r--r--  1 root  root    84 5月   6 13:27 batch.sh</span><br><span class="line">-rwxrwxrwx  1 james james   32 5月   6 13:06 helloworld.sh*</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# bash batch.sh</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# ll</span><br><span class="line">总用量 20</span><br><span class="line">drwxr-xr-x  2 james james 4096 5月   6 13:29 ./</span><br><span class="line">drwxr-xr-x 22 james james 4096 5月   6 13:06 ../</span><br><span class="line">-rw-r--r--  1 root  root    84 5月   6 13:27 batch.sh</span><br><span class="line">-rw-r--r--  1 root  root    15 5月   6 13:29 demo.txt</span><br><span class="line">-rwxrwxrwx  1 james james   32 5月   6 13:06 helloworld.sh*</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# cat demo.txt</span><br><span class="line">this is a demo</span><br><span class="line">root@james-Vostro-5471:/home/james/datas# cat batch.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">cd /home/james/datas</span><br><span class="line">touch demo.txt</span><br><span class="line">echo &quot;this is a demo&quot; &gt;&gt; demo.txt</span><br></pre></td></tr></table></figure>



<h3 id="Shell中的变量"><a href="#Shell中的变量" class="headerlink" title="Shell中的变量"></a>Shell中的变量</h3><h4 id="常用系统变量"><a href="#常用系统变量" class="headerlink" title="常用系统变量"></a>常用系统变量</h4><p>&ensp;&ensp;&ensp;&ensp;$HOME(根目录) 、$PWD(当前目录)、 $SHELL(shell解析器目录)、 $USER(当前用户)等</p>
<p>&ensp;&ensp;&ensp;&ensp;范例：查看系统变量的值</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ echo $HOME</span><br><span class="line">/home/james</span><br><span class="line">james@james-Vostro-5471:~$ echo $PWD</span><br><span class="line">/home/james</span><br><span class="line">james@james-Vostro-5471:~$ echo $SHELL</span><br><span class="line">/bin/bash</span><br><span class="line">james@james-Vostro-5471:~$ echo $USER</span><br><span class="line">james</span><br></pre></td></tr></table></figure>

<h4 id="自定义变量"><a href="#自定义变量" class="headerlink" title="自定义变量"></a>自定义变量</h4><ul>
<li><p>定义变量 ： 变量=值（声明变量时等号左右不能有空格）</p>
  <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ M=1</span><br><span class="line">james@james-Vostro-5471:~$ echo $M</span><br><span class="line">1</span><br></pre></td></tr></table></figure></li>
<li><p>撤销变量：unset 变量</p>
  <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ unset M</span><br><span class="line">james@james-Vostro-5471:~$ echo $M</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>声明静态变量：readonly 变量，注意：不能unset</p>
  <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ readonly B=3</span><br><span class="line">james@james-Vostro-5471:~$ echo $B</span><br><span class="line">3</span><br><span class="line">james@james-Vostro-5471:~$ unset B</span><br><span class="line">bash: unset: B: 无法取消设定: 只读 variable</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h4 id="变量定义规则"><a href="#变量定义规则" class="headerlink" title="变量定义规则"></a>变量定义规则</h4><ul>
<li>变量名称可以由字母、数字、下划线组成，但是不能以数字开头，环境变量名建议大写。</li>
<li>等号两侧不能有空格</li>
<li>在bash中，变量默认类型都是字符串类型，无法直接进行数值运算</li>
<li>变量的值如果有空格，需要使用双引号或单引号括起来</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ S=1+1</span><br><span class="line">james@james-Vostro-5471:~$ echo $S</span><br><span class="line">1+1</span><br><span class="line">james@james-Vostro-5471:~$ d=zx 12 3</span><br><span class="line">12：未找到命令</span><br><span class="line">james@james-Vostro-5471:~$ d=&#x27;zx 12 3&#x27;</span><br><span class="line">james@james-Vostro-5471:~$ echo $d</span><br><span class="line">zx 12 3</span><br><span class="line">james@james-Vostro-5471:~$ </span><br></pre></td></tr></table></figure>

<h4 id="提升全局变量"><a href="#提升全局变量" class="headerlink" title="提升全局变量"></a>提升全局变量</h4><p>&ensp;&ensp;&ensp;&ensp;把局部变量提升为全局变量，可供其他shell程序使用。</p>
<p>&ensp;&ensp;&ensp;&ensp;export 变量名</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ vim helloworld.sh</span><br><span class="line">james@james-Vostro-5471:~/datas$ cat helloworld.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">echo &quot;hello world&quot;</span><br><span class="line"></span><br><span class="line">echo $D</span><br><span class="line">james@james-Vostro-5471:~/datas$ D=&#x27;bian liang&#x27;</span><br><span class="line">james@james-Vostro-5471:~/datas$ echo $D</span><br><span class="line">bian liang</span><br><span class="line">james@james-Vostro-5471:~/datas$ sh helloworld.sh</span><br><span class="line">hello world</span><br><span class="line"></span><br><span class="line">james@james-Vostro-5471:~/datas$ export D</span><br><span class="line">james@james-Vostro-5471:~/datas$ sh helloworld.sh</span><br><span class="line">hello world</span><br><span class="line">bian liang</span><br><span class="line">james@james-Vostro-5471:~/datas$ </span><br></pre></td></tr></table></figure>

<h4 id="特殊变量"><a href="#特殊变量" class="headerlink" title="特殊变量"></a>特殊变量</h4><p>1、$n</p>
<p>&ensp;&ensp;&ensp;&ensp;功能描述：n为数字，$0代表该脚本名称，包含完整路径。$1- 9 代表第一到第九个参数 ，十 以上的参数需要用大括号包含，如{10}。</p>
<p>&ensp;&ensp;&ensp;&ensp;范例：输出该脚本文件名称，输入参数1和输入参数2的值</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ touch parameter.sh</span><br><span class="line">james@james-Vostro-5471:~/datas$ vim parameter.sh</span><br><span class="line">james@james-Vostro-5471:~/datas$ cat parameter.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">echo &quot;$0 $1 $2 $3&quot;</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash parameter.sh</span><br><span class="line">parameter.sh   </span><br><span class="line">james@james-Vostro-5471:~/datas$ bash parameter.sh first</span><br><span class="line">parameter.sh first  </span><br><span class="line">james@james-Vostro-5471:~/datas$ bash parameter.sh first second</span><br><span class="line">parameter.sh first second </span><br><span class="line">james@james-Vostro-5471:~/datas$ bash parameter.sh first second third</span><br><span class="line">parameter.sh first second third</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash parameter.sh first second third fourth</span><br><span class="line">parameter.sh first second third</span><br><span class="line">james@james-Vostro-5471:~/datas$ </span><br></pre></td></tr></table></figure>

<p>2、$#</p>
<p>&ensp;&ensp;&ensp;&ensp;功能描述：获取所有输入参数个数，常用于循环。</p>
<p>&ensp;&ensp;&ensp;&ensp;范例：获取输入参数的个数</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ vim parameter.sh</span><br><span class="line">james@james-Vostro-5471:~/datas$ cat parameter.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">echo &quot;$0 $1 $2 $3&quot;</span><br><span class="line"></span><br><span class="line">echo $#</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash parameter.sh first second third</span><br><span class="line">parameter.sh first second third</span><br><span class="line">3</span><br><span class="line">james@james-Vostro-5471:~/datas$ </span><br></pre></td></tr></table></figure>

<p>3、$*和$@</p>
<p>&ensp;&ensp;&ensp;&ensp;$* : 这个变量代表命令行中所有的参数，$*把所有的参数看成一个整体</p>
<p>&ensp;&ensp;&ensp;&ensp;$@ : 这个变量也代表命令行中所有的参数，不过$@把每个参数区分对待</p>
<p>&ensp;&ensp;&ensp;&ensp;范例：打印输出所有参数</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ vim parameter.sh</span><br><span class="line">james@james-Vostro-5471:~/datas$ cat parameter.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">echo &quot;$0 $1 $2 $3&quot;</span><br><span class="line"></span><br><span class="line">echo $#</span><br><span class="line">echo $*</span><br><span class="line">echo $@</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash parameter.sh first second third</span><br><span class="line">parameter.sh first second third</span><br><span class="line">3</span><br><span class="line">first second third</span><br><span class="line">first second third</span><br><span class="line">james@james-Vostro-5471:~/datas$ </span><br></pre></td></tr></table></figure>

<p>4、$?</p>
<p>&ensp;&ensp;&ensp;&ensp;功能描述：最后一次执行的命令的返回状态。如果这个变量的值为0，证明上一个命令正确执行；如果这个变量的值为非0（具体哪个数，由命令自己来决定），则证明上一个命令执行不正确了。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ $?</span><br><span class="line">0：未找到命令</span><br><span class="line">james@james-Vostro-5471:~/datas$ echo $?</span><br><span class="line">127</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash helloworld.sh</span><br><span class="line">hello world</span><br><span class="line">bian liang</span><br><span class="line">james@james-Vostro-5471:~/datas$ echo $?</span><br><span class="line">0</span><br></pre></td></tr></table></figure>

<h3 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h3><h4 id="1、基本语法："><a href="#1、基本语法：" class="headerlink" title="1、基本语法："></a>1、基本语法：</h4><ol>
<li><p>“$((运算式))”或“$[运算式]”</p>
</li>
<li><p>expr  +，-，*，/，%  加，减，乘，除，取余</p>
</li>
</ol>
<p>&ensp;&ensp;&ensp;&ensp;注意：expr运算符间要有空格</p>
<h4 id="2、范例：计算3-2"><a href="#2、范例：计算3-2" class="headerlink" title="2、范例：计算3+2"></a>2、范例：计算3+2</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ expr 3 + 2</span><br><span class="line">5</span><br><span class="line">james@james-Vostro-5471:~/datas$ s=$((3+2))</span><br><span class="line">james@james-Vostro-5471:~/datas$ echo $s</span><br><span class="line">5</span><br></pre></td></tr></table></figure>

<p>范例：计算（2+3）*4</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ d=$[(2+3)*4]</span><br><span class="line">james@james-Vostro-5471:~/datas$ echo $d</span><br><span class="line">20</span><br><span class="line">james@james-Vostro-5471:~/datas$ expr `expr 2 + 3` \* 4</span><br><span class="line">20</span><br></pre></td></tr></table></figure>

<h3 id="条件判断"><a href="#条件判断" class="headerlink" title="条件判断"></a>条件判断</h3><h4 id="1、语法："><a href="#1、语法：" class="headerlink" title="1、语法："></a>1、语法：</h4><p>[ condition ] (<strong>condition前后要有空格</strong>)</p>
<p>注意：条件非空即为true，如[z]返回true，[] 返回false</p>
<h4 id="2、常用条件判断"><a href="#2、常用条件判断" class="headerlink" title="2、常用条件判断"></a>2、常用条件判断</h4><p>（1）两个整数之间比较</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#x3D; 字符串比较</span><br><span class="line">-lt 小于（less than） -le 小于等于 （less equal）</span><br><span class="line">-eq 等于 （equal） -gt 大于 （greater than）</span><br><span class="line">-ge 大于等于 （greater equal） -ne不等于 （Not equal）</span><br></pre></td></tr></table></figure>

<p>（2）按照文件权限进行判断</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">-r 有读的权限（read） -w 有写的权限 （write）</span><br><span class="line">-x 有执行的权限 （execute）</span><br></pre></td></tr></table></figure>

<p>（3）按照文件类型进行判断</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">-f 文件存在并且是一个常规文件（file）</span><br><span class="line">-e 文件存在 （existence）</span><br><span class="line">-d 文件存在并是一个目录（directory）</span><br></pre></td></tr></table></figure>

<p>范例：23是否大于等于22</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ [ 23 -ge 22 ]</span><br><span class="line">james@james-Vostro-5471:~/datas$ echo $?</span><br><span class="line">0</span><br><span class="line">james@james-Vostro-5471:~/datas$ [ 23 -le 22 ]</span><br><span class="line">james@james-Vostro-5471:~/datas$ echo $?</span><br><span class="line">1</span><br></pre></td></tr></table></figure>

<p>范例：查看helloworld.sh 是否具有执行权限</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ [ -x /home/james/datas/helloworld.sh ]</span><br><span class="line">james@james-Vostro-5471:~/datas$ echo $?</span><br><span class="line">0</span><br></pre></td></tr></table></figure>

<p>范例：判断/home/james/datas/hello.txt是否存在</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ [ -e /home/james/datas/hello.txt ]</span><br><span class="line">james@james-Vostro-5471:~/datas$ echo $?</span><br><span class="line">1</span><br><span class="line">james@james-Vostro-5471:~/datas$ [ -e /home/james/datas/helloworld.sh ]</span><br><span class="line">james@james-Vostro-5471:~/datas$ echo $?</span><br><span class="line">0</span><br></pre></td></tr></table></figure>

<p>（4）多条件判断（&amp;&amp;表示前一条命令执行成功时，才执行后一条命令，|| 表示上一条命令执行失败后，才执行下一条命令）[]</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ [ 3 -ge 2 ] &amp;&amp; echo ok || echo notok</span><br><span class="line">ok</span><br><span class="line">james@james-Vostro-5471:~/datas$ [ 3 -ge 2 ] &amp;&amp; [ ] || echo notok</span><br><span class="line">notok</span><br><span class="line">james@james-Vostro-5471:~/datas$ [ 3 -ge 2 ] &amp; echo ok</span><br><span class="line">[1] 27351</span><br><span class="line">ok</span><br></pre></td></tr></table></figure>

<h3 id="流程控制"><a href="#流程控制" class="headerlink" title="流程控制"></a>流程控制</h3><h4 id="1、if判断"><a href="#1、if判断" class="headerlink" title="1、if判断"></a>1、if判断</h4><p>语法：</p>
<pre><code>if [ 条件判断式 ]；then
 程序
 fi
 或者
 if [ 条件判断式 ]
 then
 程序
 fi
</code></pre>
<p>注意事项：</p>
<ul>
<li>[ 条件判断式 ] ，中括号和条件判断式之间必须有空格</li>
<li>if后要有空格</li>
</ul>
<p>范例：输入一个数字，如果是1，则输出one，如果是2，则输出two，如果是其他，什么也不输出。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cat testIf.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">if [ $1 -eq 1 ];then</span><br><span class="line">    echo &quot;one&quot;</span><br><span class="line">elif [ $1 -eq 2 ]</span><br><span class="line">then</span><br><span class="line">    echo &quot;two&quot;</span><br><span class="line">fi</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testIf.sh</span><br><span class="line">testIf.sh: 第 3 行: [: -eq: 需要一元表达式</span><br><span class="line">testIf.sh: 第 5 行: [: -eq: 需要一元表达式</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testIf.sh 1</span><br><span class="line">one</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testIf.sh 2</span><br><span class="line">two</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testIf.sh 3</span><br></pre></td></tr></table></figure>

<h4 id="2、case语句"><a href="#2、case语句" class="headerlink" title="2、case语句"></a>2、case语句</h4><p>语法：</p>
<pre><code>case $变量名 in
“值1”）
如果变量的值等于1，则执行程序1
;;
“值2”）
 如果变量的值等于2，则执行程序2
;;
…省略其他分支…
*)
 如果变量的值都不是以上的值，则执行此程序
;;
esac
</code></pre>
<p>注意事项：</p>
<ul>
<li><p>case行尾必须为单词“in”，每一个模式匹配必须以右括号“）”结束。</p>
</li>
<li><p>双分号“;;”表示命令序列结束，相当于java中的break。</p>
</li>
<li><p>最后的“*）”表示默认模式，相当于java中的default</p>
</li>
</ul>
<p>范例：输入一个数字，如果是1，则输出one，如果是2，则输出two，如果是其他，输出hello</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cat testCase.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">case $1 in</span><br><span class="line">1)</span><br><span class="line">    echo &quot;one&quot;</span><br><span class="line">;;</span><br><span class="line">2)</span><br><span class="line">    echo &quot;two&quot;</span><br><span class="line">;;</span><br><span class="line">*)</span><br><span class="line">    echo &quot;hello&quot;</span><br><span class="line">;;</span><br><span class="line">esac</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testCase.sh 1</span><br><span class="line">one</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testCase.sh 2</span><br><span class="line">two</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testCase.sh 3</span><br><span class="line">hello</span><br></pre></td></tr></table></figure>

<h4 id="3、for循环"><a href="#3、for循环" class="headerlink" title="3、for循环"></a>3、for循环</h4><p>语法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">for ((初始值；循环控制条件；变量变化))</span><br><span class="line"> do</span><br><span class="line"> 程序</span><br><span class="line"> done</span><br></pre></td></tr></table></figure>

<p>范例：从1加到100</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cat testFor.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">s=0</span><br><span class="line">for ((i=1;i&lt;=100;++i))</span><br><span class="line">do</span><br><span class="line">    s=$[$s+$i] </span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">echo $s</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testFor.sh</span><br><span class="line">5050</span><br></pre></td></tr></table></figure>

<p>语法2：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">for 变量 in 值1 值2 值3…</span><br><span class="line"> do</span><br><span class="line"> 程序</span><br><span class="line"> done</span><br></pre></td></tr></table></figure>

<p>范例：打印所有输入参数（观察 ∗ 与 *与 ∗与@的区别）</p>
<p>使用$*</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cat testFor.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">s=0</span><br><span class="line">for i in $* </span><br><span class="line">do</span><br><span class="line">    echo &quot;打印所有参数：$i&quot; </span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testFor.sh a c v r </span><br><span class="line">打印所有参数：a</span><br><span class="line">打印所有参数：c</span><br><span class="line">打印所有参数：v</span><br><span class="line">打印所有参数：r</span><br></pre></td></tr></table></figure>

<p>使用$@</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cat testFor.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">s=0</span><br><span class="line">for i in $@ </span><br><span class="line">do</span><br><span class="line">    echo &quot;打印所有参数：$i&quot; </span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testFor.sh</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testFor.sh adda sda</span><br><span class="line">打印所有参数：adda</span><br><span class="line">打印所有参数：sda</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cat testFor.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">for i in &quot;$*&quot; </span><br><span class="line">do</span><br><span class="line">    echo &quot;打印所有参数：$i&quot; </span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">for j in &quot;$@&quot;</span><br><span class="line">do</span><br><span class="line">    echo &quot;打印参数：$j&quot;</span><br><span class="line">done    </span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testFor.sh a b c</span><br><span class="line">打印所有参数：a b c</span><br><span class="line">打印参数：a</span><br><span class="line">打印参数：b</span><br><span class="line">打印参数：c</span><br></pre></td></tr></table></figure>



<p>&ensp;&ensp;&ensp;&ensp;总结：由上图可以看出” ∗ “ 将 所 有 参 数 看 做 一 个 整 体 输 出 ， 只 循 环 一 次 ； 而 “ *”将所有参数看做一个整体输出，只循环一次；而” ∗”将所有参数看做一个整体输出，只循环一次；而”@”将输出参数分开输出，执行三次循环。</p>
<h4 id="4、while循环"><a href="#4、while循环" class="headerlink" title="4、while循环"></a>4、while循环</h4><p>语法： </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">while [ 条件判断式 ]</span><br><span class="line"> do</span><br><span class="line"> 程序</span><br><span class="line"> done</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>范例：从1加到100</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cat testWhile.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">s=0</span><br><span class="line">i=1</span><br><span class="line">while [ $i -le 100 ]</span><br><span class="line">do</span><br><span class="line">    s=$[$s + $i]</span><br><span class="line">    i=$[$i + 1]</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">echo $s</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testWhile.sh</span><br><span class="line">5050</span><br></pre></td></tr></table></figure>

<h3 id="read读取控制台输入"><a href="#read读取控制台输入" class="headerlink" title="read读取控制台输入"></a>read读取控制台输入</h3><p>语法：read 选项 参数</p>
<p>选项：</p>
<ul>
<li>-p：指定读取值时的提示符</li>
<li>-t ：指定读取值时的时间（秒）</li>
</ul>
<p>参数：</p>
<ul>
<li>变量：指定读取值的变量名</li>
</ul>
<p>范例：提示7秒内，读取控制台输入的名称</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cat testRead.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">read -t 7 -p &quot;input your name: &quot; NAME</span><br><span class="line">echo $NAME</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash testRead.sh</span><br><span class="line">input your name: 123456</span><br><span class="line">123456</span><br></pre></td></tr></table></figure>

<h3 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h3><h4 id="系统函数"><a href="#系统函数" class="headerlink" title="系统函数"></a>系统函数</h4><p>1、basename基本语法</p>
<p>&ensp;&ensp;&ensp;&ensp;basename [string/pathname] suffix </p>
<p>&ensp;&ensp;&ensp;&ensp;功能描述：basename命令会删掉所有的前缀包括最后一个（‘/’）字符，然后将字符显示出来。</p>
<p>&ensp;&ensp;&ensp;&ensp;选项：suffix为后缀，如果suffix被指定了，basename会将pathname或string中的suffix去掉</p>
<p>范例：截取/home/zby/read.sh路径的文件名称</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ basename /home/james/datas/helloworld.sh</span><br><span class="line">helloworld.sh</span><br><span class="line">james@james-Vostro-5471:~/datas$ basename /home/james/datas/testIf.sh .sh</span><br><span class="line">testIf</span><br></pre></td></tr></table></figure>



<p>2、dirrame基本语法</p>
<p>&ensp;&ensp;&ensp;&ensp;dirname 文件绝对路径 </p>
<p>&ensp;&ensp;&ensp;&ensp;功能描述：从给定的包含绝对路径的文件名中去除文件名（非目录的部分），然后返回剩下的路径（目录的部分）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ dirname /home/james/datas/testIf.sh</span><br><span class="line">/home/james/datas</span><br></pre></td></tr></table></figure>

<h4 id="自定义函数"><a href="#自定义函数" class="headerlink" title="自定义函数"></a>自定义函数</h4><p>1、基本语法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[function] funname [()]</span><br><span class="line">&#123;</span><br><span class="line"> Action;</span><br><span class="line"> [return int;]</span><br><span class="line">&#125;</span><br><span class="line">funname</span><br></pre></td></tr></table></figure>


<p>2、经验技巧</p>
<p>&ensp;&ensp;&ensp;&ensp;必须在调用函数的地方之前，先声明函数，shell脚本是逐行运行。不会像其他语言一样先编译。<br>&ensp;&ensp;&ensp;&ensp;函数返回值，只能通过<code>$?</code>系统变量获得，可以显示加：return返回，如果不加，将以最后一条命令运行结果，作为返回值。return后跟数值n（0-255）</p>
<p>范例：计算两个输入参数的和</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cat fun.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">function sum()</span><br><span class="line">&#123;</span><br><span class="line">    s=0;</span><br><span class="line">    s=$[$1+$2]</span><br><span class="line">    echo $s</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">read -p &quot;input parameter1:&quot; p1</span><br><span class="line">read -p &quot;input parameter2:&quot; p2</span><br><span class="line"></span><br><span class="line">sum $p1 $p2</span><br><span class="line">james@james-Vostro-5471:~/datas$ bash fun.sh</span><br><span class="line">input parameter1:1</span><br><span class="line">input parameter2:2</span><br><span class="line">3</span><br></pre></td></tr></table></figure>

<h3 id="Shell工具"><a href="#Shell工具" class="headerlink" title="Shell工具"></a>Shell工具</h3><h4 id="cut"><a href="#cut" class="headerlink" title="cut"></a>cut</h4><p>&ensp;&ensp;&ensp;&ensp;cut的工作就是“剪”，具体地说就是在文件中负责剪切数据用的。cut命令从文件中的每一行剪切文字、字符和字段并将这些字节、字符和字段输出。</p>
<p>1、基本用法：</p>
<ul>
<li>cut [选项参数] filename</li>
<li>说明：默认分隔符是制表符</li>
</ul>
<p>2、参数说明：</p>
<ul>
<li>-f  列号，提取第几列</li>
<li>-d 分隔符，按照指定分隔符分割lie</li>
</ul>
<p>范例：数据准备</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ vim cut.txt</span><br><span class="line">james@james-Vostro-5471:~/datas$ cat cut.txt</span><br><span class="line">bei jing sheng yang</span><br><span class="line">shang hai xiang gang</span><br><span class="line">tian jin xia men</span><br><span class="line">wu han tai bei</span><br><span class="line">zheng zhou</span><br><span class="line">fu zhou</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;切割<code>cut.txt</code>的第一列</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cut -d &quot; &quot; -f 1 cut.txt</span><br><span class="line">bei</span><br><span class="line">shang</span><br><span class="line">tian</span><br><span class="line">wu</span><br><span class="line">zheng</span><br><span class="line">fu</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;切割<code>cut.txt</code>的第2，3列</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cut -d &quot; &quot; -f 2,3 cut.txt</span><br><span class="line">jing sheng</span><br><span class="line">hai xiang</span><br><span class="line">jin xia</span><br><span class="line">han tai</span><br><span class="line">zhou</span><br><span class="line">zhou</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;切割<code>cut.txt</code>文件中的<code>shang</code></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cat cut.txt | grep shang</span><br><span class="line">shang hai xiang gang</span><br><span class="line">james@james-Vostro-5471:~/datas$ cat cut.txt | grep shang | cut -d &quot; &quot; -f 1</span><br><span class="line">shang</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;选取系统<code>PATH</code>变量值，第二个“：”开始后的所有路径：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ echo $PATH</span><br><span class="line">/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin</span><br><span class="line">james@james-Vostro-5471:~/datas$ echo $PATH | cut -d : -f 3-</span><br><span class="line">/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;切割<code>ifconfig</code>后打印<code>IP</code>地址</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ ifconfig lo</span><br><span class="line">lo: flags=73&lt;UP,LOOPBACK,RUNNING&gt;  mtu 65536</span><br><span class="line">        inet 127.0.0.1  netmask 255.0.0.0</span><br><span class="line">        inet6 ::1  prefixlen 128  scopeid 0x10&lt;host&gt;</span><br><span class="line">        loop  txqueuelen 1000  (本地环回)</span><br><span class="line">        RX packets 44305  bytes 5010333 (5.0 MB)</span><br><span class="line">        RX errors 0  dropped 0  overruns 0  frame 0</span><br><span class="line">        TX packets 44305  bytes 5010333 (5.0 MB)</span><br><span class="line">        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0</span><br><span class="line"></span><br><span class="line">james@james-Vostro-5471:~$ ifconfig lo | grep &quot;inet &quot;</span><br><span class="line">        inet 127.0.0.1  netmask 255.0.0.0</span><br><span class="line">james@james-Vostro-5471:~$ ifconfig lo | grep &quot;inet &quot; | cut -d &quot; &quot; -f 10</span><br><span class="line">127.0.0.1</span><br></pre></td></tr></table></figure>

<h4 id="sed"><a href="#sed" class="headerlink" title="sed"></a>sed</h4><p>&ensp;&ensp;&ensp;&ensp;<code>sed</code>是一种流编辑器，它一次处理一行内容。处理时，把当前处理的行存储在临时缓冲区中，称为“模式空间”，接着用<code>sed</code>命令处理缓存区中的内容，处理完成后，把缓冲区的内容送往屏幕。接着处理下一行，这样不断重复，知道文件末尾。文件内容并没有改变，除非你使用重定向存储输出。</p>
<ul>
<li><p>基本用法：</p>
<p>  sed [选项参数] ‘command’ filename</p>
</li>
<li><p>参数说明</p>
<p>  -e 直接在命令模式上进行sed的动作编辑</p>
</li>
<li><p>命令功能描述</p>
<p>  a 新增，a的后面可以接字符串，在下一行出现</p>
<p>  d 删除</p>
<p>  s 查找并替换</p>
</li>
</ul>
<p>范例：数据准备</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ vim sed.txt</span><br><span class="line">james@james-Vostro-5471:~$ cat sed.txt</span><br><span class="line">abcd aaaa</span><br><span class="line">zxcv aaas</span><br><span class="line">dasd qwdd</span><br><span class="line">ad ad</span><br><span class="line">dc da</span><br><span class="line">da ee</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;将<code>aa cc</code>这个字符串插入到<code>sed.txt</code>第二行下打印</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ sed &quot;2a aa cc&quot; sed.txt</span><br><span class="line">abcd aaaa</span><br><span class="line">zxcv aaas</span><br><span class="line">aa cc</span><br><span class="line">dasd qwdd</span><br><span class="line">ad ad</span><br><span class="line">dc da</span><br><span class="line">da ee</span><br><span class="line">james@james-Vostro-5471:~$ cat sed.txt</span><br><span class="line">abcd aaaa</span><br><span class="line">zxcv aaas</span><br><span class="line">dasd qwdd</span><br><span class="line">ad ad</span><br><span class="line">dc da</span><br><span class="line">da ee</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;删除 <code>sed.txt</code> 中包含<code>d</code>的行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ sed &quot;/d/d&quot; sed.txt</span><br><span class="line">zxcv aaas</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;将<code>sed.txt</code>文件中的<code>aa</code>替换为<code>pp</code>(<code>g</code>表示global，全部替换)</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ sed &quot;s/aa/pp/g&quot; sed.txt</span><br><span class="line">abcd pppp</span><br><span class="line">zxcv ppas</span><br><span class="line">dasd qwdd</span><br><span class="line">ad ad</span><br><span class="line">dc da</span><br><span class="line">da ee</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;将<code>sed.txt</code>文件中的第二行删除并将<code>aaa</code>替换为<code>qf</code></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~$ sed -e &quot;2d&quot; -e &quot;s/aaa/qf/g&quot; sed.txt</span><br><span class="line">abcd qfa</span><br><span class="line">dasd qwdd</span><br><span class="line">ad ad</span><br><span class="line">dc da</span><br><span class="line">da ee</span><br></pre></td></tr></table></figure>

<h4 id="awk"><a href="#awk" class="headerlink" title="awk"></a>awk</h4><p>&ensp;&ensp;&ensp;&ensp;一个强大的文本分析工具，把文件逐行的读入，以空格为默认的分割符并将每行切片，切开的部分再进行分析处理。<br>1.基本用法</p>
<p>&ensp;&ensp;&ensp;&ensp;awk [选项参数] ‘pattern1 {action} pattern2 {action}…’ filename</p>
<p>&ensp;&ensp;&ensp;&ensp;pattern :  表示AWK在数据中查找的内容，就是匹配模式</p>
<p>&ensp;&ensp;&ensp;&ensp;action：在找到匹配内容时所执行的一系列命令。</p>
<p>参数说明</p>
<ul>
<li>-F   指定输入文件拆分隔离</li>
<li>-v   赋值一个用户定义变量</li>
</ul>
<p>范例：数据准备(/etc/passwd文件)</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ sudo cp /etc/passwd ./</span><br><span class="line">[sudo] james 的密码： </span><br><span class="line">james@james-Vostro-5471:~/datas$ ls</span><br><span class="line">batch.sh  demo.txt  helloworsudold.sh  passwd       testFor.sh  testRead.sh</span><br><span class="line">cut.txt   fun.sh    parameter.sh   testCase.sh  testIf.sh   testWhile.sh</span><br><span class="line">james@james-Vostro-5471:~/datas$ cat passwd</span><br><span class="line">root:x:0:0:root:/root:/bin/bash</span><br><span class="line">daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin</span><br><span class="line">bin:x:2:2:bin:/bin:/usr/sbin/nologin</span><br><span class="line">sys:x:3:3:sys:/dev:/usr/sbin/nologin</span><br><span class="line">sync:x:4:65534:sync:/bin:/bin/sync</span><br><span class="line">games:x:5:60:games:/usr/games:/usr/sbin/nologin</span><br><span class="line">man:x:6:12:man:/var/cache/man:/usr/sbin/nologin</span><br><span class="line">lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin</span><br><span class="line">mail:x:8:8:mail:/var/mail:/usr/sbin/nologin</span><br><span class="line">news:x:9:9:news:/var/spool/news:/usr/sbin/nologin</span><br><span class="line">uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin</span><br><span class="line">proxy:x:13:13:proxy:/bin:/usr/sbin/nologin</span><br><span class="line">www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin</span><br><span class="line">backup:x:34:34:backup:/var/backups:/usr/sbin/nologin</span><br><span class="line">list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin</span><br><span class="line">irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin</span><br><span class="line">gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin</span><br><span class="line">nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin</span><br><span class="line">systemd-network:x:100:102:systemd Network Management,,,:/run/systemd/netif:/usr/sbin/nologin</span><br><span class="line">systemd-resolve:x:101:103:systemd Resolver,,,:/run/systemd/resolve:/usr/sbin/nologin</span><br><span class="line">syslog:x:102:106::/home/syslog:/usr/sbin/nologin</span><br><span class="line">messagebus:x:103:107::/nonexistent:/usr/sbin/nologin</span><br><span class="line">_apt:x:104:65534::/nonexistent:/usr/sbin/nologin</span><br><span class="line">uuidd:x:105:111::/run/uuidd:/usr/sbin/nologin</span><br><span class="line">avahi-autoipd:x:106:112:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/usr/sbin/nologin</span><br><span class="line">usbmux:x:107:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin</span><br><span class="line">dnsmasq:x:108:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin</span><br><span class="line">rtkit:x:109:114:RealtimeKit,,,:/proc:/usr/sbin/nologin</span><br><span class="line">cups-pk-helper:x:110:116:user for cups-pk-helper service,,,:/home/cups-pk-helper:/usr/sbin/nologin</span><br><span class="line">speech-dispatcher:x:111:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false</span><br><span class="line">whoopsie:x:112:117::/nonexistent:/bin/false</span><br><span class="line">kernoops:x:113:65534:Kernel Oops Tracking Daemon,,,:/:/usr/sbin/nologin</span><br><span class="line">saned:x:114:119::/var/lib/saned:/usr/sbin/nologin</span><br><span class="line">pulse:x:115:120:PulseAudio daemon,,,:/var/run/pulse:/usr/sbin/nologin</span><br><span class="line">avahi:x:116:122:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/usr/sbin/nologin</span><br><span class="line">colord:x:117:123:colord colour management daemon,,,:/var/lib/colord:/usr/sbin/nologin</span><br><span class="line">hplip:x:118:7:HPLIP system user,,,:/var/run/hplip:/bin/false</span><br><span class="line">geoclue:x:119:124::/var/lib/geoclue:/usr/sbin/nologin</span><br><span class="line">gnome-initial-setup:x:120:65534::/run/gnome-initial-setup/:/bin/false</span><br><span class="line">gdm:x:121:125:Gnome Display Manager:/var/lib/gdm3:/bin/false</span><br><span class="line">james:x:1000:1000:James,,,:/home/james:/bin/bash</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;搜索<code>passwd</code>文件以<code>root</code>关键字开头的所有行，并输出该行的第一列和第七列，中间以“，”号分割。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ awk -F : &#x27;/^root/ &#123;print $7&#125;&#x27; passwd</span><br><span class="line">/bin/bash</span><br><span class="line">james@james-Vostro-5471:~/datas$ awk -F : &#x27;/^root/ &#123;print $1&quot;,&quot;$7&#125;&#x27; passwd</span><br><span class="line">root,/bin/bash</span><br><span class="line">james@james-Vostro-5471:~/datas$ </span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;只显示<code>passwd</code>的第一列和第七列，以逗号分割，且在所有行前面添加列名<code>user</code>，<code>shell</code> 在最后一行添加<code>gqw, /bin/zuihsuai</code>。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ awk -F : &#x27;BEGIN&#123;print &quot;user,shell&quot;&#125;&#123;print $1&quot;,&quot;$7&#125; END&#123;print &quot;gqw,/bin/zuihuai&quot;&#125;&#x27; passwd</span><br><span class="line">user,shell</span><br><span class="line">root,/bin/bash</span><br><span class="line">daemon,/usr/sbin/nologin</span><br><span class="line">bin,/usr/sbin/nologin</span><br><span class="line">sys,/usr/sbin/nologin</span><br><span class="line">sync,/bin/sync</span><br><span class="line">games,/usr/sbin/nologin</span><br><span class="line">man,/usr/sbin/nologin</span><br><span class="line">lp,/usr/sbin/nologin</span><br><span class="line">mail,/usr/sbin/nologin</span><br><span class="line">news,/usr/sbin/nologin</span><br><span class="line">uucp,/usr/sbin/nologin</span><br><span class="line">…………………………………………………………………………………………</span><br></pre></td></tr></table></figure>

<p>注意：</p>
<ul>
<li><code>BEGIN</code>在所有数据读取行之前执行</li>
<li><code>END</code> 在所有数据执行之后执行</li>
</ul>
<p>&ensp;&ensp;&ensp;&ensp;将passwd文件中的用户id增加数值1并输出</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ awk -F : &#x27;&#123;print $3&#125;&#x27; passwd</span><br><span class="line">0</span><br><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">………………………………………………………………………………………………………………</span><br><span class="line">james@james-Vostro-5471:~/datas$ awk -F : -v i=1 &#x27;&#123;print $3+i&#125;&#x27; passwd</span><br><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">…………………………………………………………………………………………………………………………</span><br></pre></td></tr></table></figure>

<p>2.<code>awk</code>的内置变量 </p>
<table>
<thead>
<tr>
<th align="center">变量</th>
<th align="center">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">FILENAME</td>
<td align="center">文件名</td>
</tr>
<tr>
<td align="center">NR</td>
<td align="center">已读的记录数</td>
</tr>
<tr>
<td align="center">NF</td>
<td align="center">浏览记录的域的个数（切割后，列的个数）</td>
</tr>
</tbody></table>
<p>范例：统计<code>passwd</code>文件名，每行的行号，每行的列数。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ awk -F : &#x27;&#123;print FILENAME&quot;,&quot;NR&quot;,&quot;NF&#125;&#x27; passwd</span><br><span class="line">passwd,1,7</span><br><span class="line">passwd,2,7</span><br><span class="line">passwd,3,7</span><br><span class="line">passwd,4,7</span><br><span class="line">passwd,5,7</span><br><span class="line">passwd,6,7</span><br><span class="line">passwd,7,7</span><br><span class="line">passwd,8,7</span><br><span class="line">………………………………………………………………</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;切割<code>inet</code></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ ifconfig lo |grep &quot;inet &quot;</span><br><span class="line">        inet 127.0.0.1  netmask 255.0.0.0</span><br><span class="line">james@james-Vostro-5471:~/datas$ ifconfig lo |grep &quot;inet &quot; | awk -F &quot; &quot; &#x27;&#123;print $2&#125;&#x27;</span><br><span class="line">127.0.0.1</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;查询<code>cut.txt</code>中空行所在的行号</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ cat cut.txt</span><br><span class="line"></span><br><span class="line">bei jing sheng yang</span><br><span class="line"></span><br><span class="line">shang hai xiang gang</span><br><span class="line">tian jin xia men</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">wu han tai bei</span><br><span class="line">zheng zhou</span><br><span class="line">fu zhou</span><br><span class="line">james@james-Vostro-5471:~/datas$ awk &#x27;/^$/&#123;print NR&#125;&#x27; cut.txt</span><br><span class="line">1</span><br><span class="line">3</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td></tr></table></figure>

<h4 id="sort"><a href="#sort" class="headerlink" title="sort"></a>sort</h4><p>&ensp;&ensp;&ensp;&ensp;sort命令是在Linux里非常有用，它将文件进行排序，并将排序的结果标准输出。</p>
<p>基本语法： sort 选项 参数</p>
<ul>
<li>-n 依照数值大小排序</li>
<li>-r 以相反的顺序来排序</li>
<li>-t 设置排序时所用的分隔字符</li>
<li>-k 指定需要排序的列</li>
</ul>
<p>&ensp;&ensp;&ensp;&ensp;参数： 指定待排序的文件列表</p>
<p>范例：</p>
<p>数据准备：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ vim sort.txt</span><br><span class="line">james@james-Vostro-5471:~/datas$ cat sort.txt</span><br><span class="line">aa:12:5.4</span><br><span class="line">ba:14:67</span><br><span class="line">kl:421:4</span><br><span class="line">er:00:5</span><br><span class="line">hj:213:00</span><br></pre></td></tr></table></figure>

<p>按照 “:” 分割后的第三列倒序排序</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ sort -t : -k 3 -n sort.txt</span><br><span class="line">hj:213:00</span><br><span class="line">kl:421:4</span><br><span class="line">er:00:5</span><br><span class="line">aa:12:5.4</span><br><span class="line">ba:14:67</span><br></pre></td></tr></table></figure>

<p>用第二列的数值进行倒序排序</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">james@james-Vostro-5471:~/datas$ sort -t : -nrk 2 sort.txt</span><br><span class="line">kl:421:4</span><br><span class="line">hj:213:00</span><br><span class="line">ba:14:67</span><br><span class="line">aa:12:5.4</span><br><span class="line">er:00:5</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/myblog/tags/shell/" rel="tag"># shell</a>
              <a href="/myblog/tags/linux/" rel="tag"># linux</a>
              <a href="/myblog/tags/leetcode/" rel="tag"># leetcode</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/myblog/2021/05/05/%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%9A%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96/" rel="prev" title="二叉树中的序列化和反序列化">
      <i class="fa fa-chevron-left"></i> 二叉树中的序列化和反序列化
    </a></div>
      <div class="post-nav-item">
    <a href="/myblog/2021/05/06/Shell%EF%BC%9ALeetcode%E7%9A%84Shell%E4%B9%A0%E9%A2%98/" rel="next" title="leetcode中的Shell习题">
      leetcode中的Shell习题 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Shell%E6%A6%82%E8%BF%B0"><span class="nav-text">Shell概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Shell%E8%A7%A3%E6%9E%90%E5%99%A8"><span class="nav-text">Shell解析器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Shell%E8%84%9A%E6%9C%AC%E5%85%A5%E9%97%A8"><span class="nav-text">Shell脚本入门</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%84%9A%E6%9C%AC%E6%A0%BC%E5%BC%8F"><span class="nav-text">脚本格式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%84%9A%E6%9C%AC%E7%9A%84%E5%B8%B8%E7%94%A8%E6%89%A7%E8%A1%8C%E6%96%B9%E5%BC%8F"><span class="nav-text">脚本的常用执行方式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A4%9A%E5%91%BD%E4%BB%A4%E5%A4%84%E7%90%86"><span class="nav-text">多命令处理</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Shell%E4%B8%AD%E7%9A%84%E5%8F%98%E9%87%8F"><span class="nav-text">Shell中的变量</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E7%B3%BB%E7%BB%9F%E5%8F%98%E9%87%8F"><span class="nav-text">常用系统变量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F"><span class="nav-text">自定义变量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8F%98%E9%87%8F%E5%AE%9A%E4%B9%89%E8%A7%84%E5%88%99"><span class="nav-text">变量定义规则</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%8F%90%E5%8D%87%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F"><span class="nav-text">提升全局变量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%89%B9%E6%AE%8A%E5%8F%98%E9%87%8F"><span class="nav-text">特殊变量</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1%E3%80%81%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95%EF%BC%9A"><span class="nav-text">1、基本语法：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2%E3%80%81%E8%8C%83%E4%BE%8B%EF%BC%9A%E8%AE%A1%E7%AE%973-2"><span class="nav-text">2、范例：计算3+2</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD"><span class="nav-text">条件判断</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1%E3%80%81%E8%AF%AD%E6%B3%95%EF%BC%9A"><span class="nav-text">1、语法：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2%E3%80%81%E5%B8%B8%E7%94%A8%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD"><span class="nav-text">2、常用条件判断</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6"><span class="nav-text">流程控制</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1%E3%80%81if%E5%88%A4%E6%96%AD"><span class="nav-text">1、if判断</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2%E3%80%81case%E8%AF%AD%E5%8F%A5"><span class="nav-text">2、case语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3%E3%80%81for%E5%BE%AA%E7%8E%AF"><span class="nav-text">3、for循环</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4%E3%80%81while%E5%BE%AA%E7%8E%AF"><span class="nav-text">4、while循环</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#read%E8%AF%BB%E5%8F%96%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%BE%93%E5%85%A5"><span class="nav-text">read读取控制台输入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%BD%E6%95%B0"><span class="nav-text">函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E5%87%BD%E6%95%B0"><span class="nav-text">系统函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0"><span class="nav-text">自定义函数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Shell%E5%B7%A5%E5%85%B7"><span class="nav-text">Shell工具</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#cut"><span class="nav-text">cut</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#sed"><span class="nav-text">sed</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#awk"><span class="nav-text">awk</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#sort"><span class="nav-text">sort</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="高乾威"
      src="/myblog/images/avatar.png">
  <p class="site-author-name" itemprop="name">高乾威</p>
  <div class="site-description" itemprop="description">报我楼成秋望月，把君诗读夜回灯</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblog/archives/">
        
          <span class="site-state-item-count">91</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblog/categories/">
          
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/myblog/tags/">
          
        <span class="site-state-item-count">59</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="http://github.com/GaoQianwei" title="GitHub → http:&#x2F;&#x2F;github.com&#x2F;GaoQianwei" rel="noopener" target="_blank">GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:2966807184@qq.com" title="E-Mail → mailto:2966807184@qq.com" rel="noopener" target="_blank">E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/xue-wu-zhi-jing-75-22" title="知乎 → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;xue-wu-zhi-jing-75-22" rel="noopener" target="_blank">知乎</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/weixin_45341339" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_45341339" rel="noopener" target="_blank">CSDN</a>
      </span>
  </div>



      </div><div>
  <canvas id="canvasDiyBlock" style="width:60%;">当前浏览器不支持canvas，请更换浏览器后再试</canvas>
<script src="/myblog/js/custom/clock.js"></script>

</div>


    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2021-04 到 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">高乾威</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

<span class="post-meta-divider">|</span>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">全站共 117.2k 字</span>
</div>

<span class="post-meta-divider">|</span>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/myblog/lib/anime.min.js"></script>
  <script src="/myblog/lib/velocity/velocity.min.js"></script>
  <script src="/myblog/lib/velocity/velocity.ui.min.js"></script>

<script src="/myblog/js/utils.js"></script>

<script src="/myblog/js/motion.js"></script>


<script src="/myblog/js/schemes/pisces.js"></script>


<script src="/myblog/js/next-boot.js"></script>


  <script defer src="/myblog/lib/three/three.min.js"></script>
    <script defer src="/myblog/lib/three/canvas_lines.min.js"></script>


  




  
<script src="/myblog/js/local-search.js"></script>













  

  

</body>
</html>
